﻿
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 
//
// 文件名：MsgCodeEnum
// 文件功能描述：
//
// 
// 创建者：suzr
// 时间：2020-04-14 11:35:10
//
// 修改人：
// 时间：
// 修改说明：
//
// 修改人：
// 时间：
// 修改说明：
//
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Text;

namespace XKPlatform.Common.Enums
{
    public enum MsgCodeEnum
    {
        /// <summary>
        /// 操作成功
        /// </summary>
        [Display(Name = "Success!", Description = "操作成功")]
        Success = 0,
        /// <summary>
        /// 未知错误
        /// </summary>
        [Display(Name = "Undefined Error!", Description = "未知错误")]
        Failed = 1,
        /// <summary>
        /// 服务暂不可用
        /// </summary>
        [Display(Name = "Service Not Avaliable!", Description = "服务暂不可用")]
        NoService = 2,
        /// <summary>
        /// 未知方法
        /// </summary>
        [Display(Name = "Undefined Method", Description = "未知方法")]
        UnsupportedMethod = 3,
        /// <summary>
        /// 请求参数无效
        /// </summary>
        [Display(Name = "Params is invalid", Description = "请求参数无效")]
        InvalidParameter = 4,
        /// <summary>
        /// 读取配置文件出错
        /// </summary>
        [Display(Name = "Config is Error", Description = "读取配置文件出错")]
        ReadConfigFailed = 5,
        /// <summary>
        /// 数据库连接出错
        /// </summary>
        [Display(Name = "DataBase Connection Fail", Description = "数据库连接出错")]
        DBConectionFailed = 6,
        /// <summary>
        /// 读取数据失败
        /// </summary>
        [Display(Name = "Read Data Fail", Description = "读取数据失败")]
        ReadDataError = 7,
        /// <summary>
        /// 返回记录为空
        /// </summary>
        [Display(Name = "Record is null", Description = "返回记录为空")]
        DataNotExist = 8,
        [Display(Name = "suning.selfmarket.receiptsupplier.query", Description = "suning.selfmarket.receiptsupplier.query")]
        receiptsupplier = 9,
        [Display(Name = "suning.selfmarket.purchaseordersupplier.query", Description = "suning.selfmarket.purchaseordersupplier.query")]
        purchaseordersupplier = 10,
        [Display(Name = "suning.selfmarket.reservesupplier.query", Description = "suning.selfmarket.reservesupplier.query")]
        reservesupplier = 11,
        [Display(Name = "can not remove yourself", Description = "不能强制退出自己")]
        disableRemove = 12,
        [Display(Name = "Param is null", Description = "参数为空")]
        ParamIsNull = 13,
        [Display(Name = "operate is defined", Description = "没有权限进行此操作")]
        NoPermission = 14,
        [Display(Name = "UserNo  Exist", Description = "登录名已经存在")]
        UserNoIsExist = 15,
        [Display(Name = "  Email  Exist", Description = "邮箱已经存在")]
        EmailIsExist = 16,
        [Display(Name = "  Code  Exist", Description = "编码已经存在")]
        CodeExist = 17,
        /// <summary>
        /// 鉴权中心SysToken验证错误
        /// </summary>
        [Display(Name = "The authentication center SysToken verifies an error", Description = "鉴权中心SysToken验证错误")]
        Authen_SysToken = 1115,
        /// <summary>
        /// 身份验证错误(Token)
        /// </summary>
        [Display(Name = "Authentication error.(Token)", Description = "身份验证错误(Token)")]
        User_TokenCheck = 1102
    }
    /// <summary>
    /// 语言类型
    /// </summary>
    public enum LanguageEnum
    {
        En = 0,
        Cn = 1
    }

    public enum Interfaceparameters
    {
        /// <summary>
        /// 全量
        /// </summary>
        [Display(Name = "Total quantity!", Description = "全量")]
        Total = 1,
        /// <summary>
        /// 前一个周期
        /// </summary>
        [Display(Name = "Previous cycle!", Description = "前一个周期")]
        Previous = 2,
        /// <summary>
        /// 固定时间
        /// </summary>
        [Display(Name = "Fixed time!", Description = "固定时间")]
        Fixed = 3
    }
    /// <summary>
    /// 中英文信息模型
    /// </summary>
    public class MsgInfo
    {
        public string En { get; set; }
        public string Cn { get; set; }

        public MsgInfo(string en, string cn)
        {
            En = en;
            Cn = cn;
        }
    }

    
    public class EnumUtils
    {
        /// <summary>
        /// 根据key返回value值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetEnumValue<T>(string key)
        {
            int re = 0;
            foreach (int v in System.Enum.GetValues(typeof(T)))
            {
                if (key.Equals(System.Enum.GetName(typeof(T),v))) {
                    re = v;
                }
            }
            return re.ToString();
        }

        /// <summary>
        /// 根据枚举获取中英文信息
        /// </summary>
        /// <param name="en"></param>
        /// <param name="languageType"></param>
        /// <returns></returns>
        public static string GetEnumDesc(MsgCodeEnum en, int languageType = (int)LanguageEnum.Cn)
        {
            Type type = en.GetType();


            var disName = type.GetField(en.ToString());

            //display名字
            var name = (DisplayAttribute)disName.GetCustomAttribute(typeof(DisplayAttribute));

            if (languageType == (int)LanguageEnum.Cn)
            {
                return name.Description ?? "";
            }
            return name.Name ?? "";


        }
        /// <summary>
        /// 根据枚举code获取中英文信息
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static MsgInfo GetEnumDescAll(MsgCodeEnum en)
        {
            Type type = en.GetType();


            var disName = type.GetField(en.ToString());

            //display名字
            var name = (DisplayAttribute)disName.GetCustomAttribute(typeof(DisplayAttribute));

            MsgInfo msg = new MsgInfo(name.Description, name.Name);
            return msg;

        }
    }

}
